package com.example.security.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.security.domain.User;
import com.example.security.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
@RequiredArgsConstructor
@Service
public class CustomUserDetailsService implements UserDetailsService {

    private final UserMapper userMapper ;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> lqw  = new LambdaQueryWrapper();
        lqw.eq(User::getLoginName,username);
        User user = userMapper.selectOne(lqw);
        if (user == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
        String a=new BCryptPasswordEncoder().encode(user.getPassword());
        user.setPassword(a);
        return new org.springframework.security.core.userdetails.User(user.getLoginName(), user.getPassword(), new ArrayList<>());
    }
}

